package com.sxt.sys.service.impl;

import com.sxt.sys.constast.SysConstast;
import com.sxt.sys.domain.Permission;
import com.sxt.sys.mapper.PermissionMapper;
import com.sxt.sys.mapper.RoleMapper;
import com.sxt.sys.service.IPermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 老雷
 * @since 2019-08-10
 */
@Service
@Transactional
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Override
	public boolean removeById(Serializable id) {
		boolean delMenu = super.removeById(id);
		this.getBaseMapper().deleteRolePermissionByPid(id);
		return delMenu;
	}

	@Override
	public List<Integer> queryPermissionIdsByRoleIds(List<Integer> roleIds) {
		return this.getBaseMapper().queryPermissionIdsByRoleIds(roleIds);
	}

	@Override
	public List<String> queryPermissionCodeByUserId(Integer id) {
		List<String> permissionCodes = new ArrayList<String>();
		List<Integer> roleIds = this.roleMapper.queryRoleUserByUserId(id);
		if(roleIds==null || roleIds.size()==0) {
			return permissionCodes;
		}else {
			List<Integer> permissionIds = this.getBaseMapper().queryPermissionIdsByRoleIds(roleIds);
			if(permissionIds==null || permissionIds.size() == 0) {
				return permissionCodes;
			}else {
				QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
				queryWrapper.eq("type", SysConstast.PERMESSION_TYPE_PERMISSION);
				queryWrapper.eq("available", SysConstast.AVAILABLE_TRUE);
				queryWrapper.in("id", permissionIds);
				List<Permission> list = this.getBaseMapper().selectList(queryWrapper);
				for(Permission permission : list) {
					permissionCodes.add(permission.getPercode());
				}
				return permissionCodes;
			}
		}
			
	}
}























